package com.encryption.service.checkcode;

import java.util.Date;
import java.util.List;
import java.util.Random;

import com.encryption.dao.UserVerifyCodeDAO;
import com.encryption.model.User;
import com.encryption.model.UserVerifyCode;
import com.encryption.service.ca.util.StringUtil;
import com.encryption.util.RandomNumberUtil;
import com.encryption.util.mail.sender.MailSend;


public class SecuirtyVerifyManageImpl implements SecurityVerifyManage {
    private UserVerifyCodeDAO uvcDAO = new UserVerifyCodeDAO();

    public Boolean verify(int userId, String random) {
        
    	UserVerifyCode code = new UserVerifyCode();
		code.setCode(random);
		code.setUserId(userId);
		
		List<UserVerifyCode> codeList = UserVerifyCodeDAO.getDAO().findByExample(code);
		if(codeList != null && codeList.size() > 0){
			UserVerifyCode dbCode = codeList.get(0);
			uvcDAO.delete(dbCode);
			return true;
		}
		
		return false;
    }


    public Boolean sendVerifyCode(User user) {
        String random = RandomNumberUtil.random(6);
        UserVerifyCode uvc = new UserVerifyCode();
        uvc.setUserId(user.getId());
        uvc.setCreateDate(new Date());
        uvc.setModifyDate(uvc.getCreateDate());
        uvc.setRandom("");
        uvc.setCode(random);
        
        uvcDAO.save(uvc);
        
        MailSend.sendTextMail(user.getEmail(), "验证码", "验证码为" + random);
        return true;
    }

}
